<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\facade\Config;//引入配置

class Es extends Controller
{   
    private $allow_ip='47.88.1.190';
    private $redis;
    private $images_host;
    private $default_lang;
    private $uri;
   

    public function initialize()
    {  
      $this->redis = new \Redis();
      $this->redis->connect(config('app.redis_ip'), 6379);
      $this->redis->auth(config('app.redis_Secret'));
      $this->redis->select(3);

      $this->images_host=config('app.images_host');
      $this->uri="https://".config('app.images_host')."/";
    }


    public function set_zhu(){
      Db::table('lily_products')->field('id,part_number,url_product,class_id,img')->chunk(1000, function($product) {
          foreach ($product as $key => $value) {
            $s=explode('_', $value['url_product']);
            if(count($s)<=1){
              $url='/products/'.$value['url_product']."/";
              $data=array();
              $data['Pid']=$value['id'];
              $data['Part Number']=$value['part_number'];
              $data['Part_arr']=str_replace('-','',change_lower_symbol($value['part_number']));
              $data['url']=$url;
              if(!empty($value['img'])){
                $data['img']=$this->images_host.$value['img'];
              }else{
                $data['img']=$this->images_host.'/blog/blogdefault.png';
              }
              $data['class_id']=$value['class_id'];
              $this->redis->hMSet($value['id'],$data);
            }
          }
      });
    }

 

    public function detail(){

      Db::table('lily_products')->field('id,part_number,url_product,class_id,img')->chunk(100, function($redis_zhu) {
        /*载荷重量转速价格*/
        $disf=array('Dynamic Radial Load','Dynamic Thrust','Dynamic Thrust Load','Static Thrust Load','Dynamic Radial Rating(1 million revolutions)(C1)','Dynamic Radial Rating(One Row 90 million revolutions)(C90)','Dynamic Radial Rating(Two Row 1 million revolutions)(C1)','Static Radial Rating(C0)','Dynamic Moment','Static Thrust','Static Radial Load','Static Moment','Basic Load Ratings Cor','Basic Load Ratings Cr','Price','price','Weight','weight','Max Speed (X1000 rpm)','MAX SPEED(RPM)','Max Speed (Grease) (X1000 RPM)','Max Speed','Product Number','LILY Part No.','IKO Part No.','Smith Part No','Smith Part No.','INA Part No.','Mcgill Part No.','RBC Part No','RBC Part No.','Mass (g)','IMO Part No','LRotherde Part No','LRolix Part No','Franke Part No','INA Part No','SKF Part No','Kaydon Part No','Dynamic Load Rating','Static Load Rating','Static Load Rating Radial','Static Load Rating Aixal','Dynamic Load Rating Radial','Dynamic Load Rating Axial','Rating at 500 RPM for 3000hrs.L 10(Radial)(LB)','Rating at 500 RPM for 3000hrs.L 10(Thrust)(LB)','Basic Load Rating Co','Basic Load Rating C','Ca Basic Dynamic Load Rating Axial','Cr Basic Dynamic Load Rating Radial','Dynamic Radial Rating(1 million revolutions)','Basic dynamic load rating(C)','Basic static load rating(C0)','Basic Static Load Rating(C0a)','Basic Dynamic Load Rating(Ca)','Basic Static Load Rating(C0r)','Basic Dynamic Load Rating(Cr)');
        $disf_str='';
        foreach ($disf as $k => $v) {
          $disf_str.=$v.','; 
        }
        $disf_str=rtrim($disf_str,',');

        foreach ($redis_zhu as $key => $value) {
          $s=explode('_', $value['url_product']);
          if(count($s)<=1){
            $hMget=$this->redis->hMget($value['id'],['Pid','class_id','Part Number']);
            $fileds=Db::table('lily_right_table')->field('fileds')->where([['class_id','=',$hMget['class_id']],['fileds','not in',$disf_str]])->select();
            $fileds_array='';
            foreach ($fileds as $km=>$vm){
              $fileds_array.="'".$vm['fileds']."',";
            }
            $fileds_array=rtrim($fileds_array,',');

            /*添加*/
            if(!empty($fileds)){
              $sql="SELECT `key`,value FROM lily_products_detail WHERE pid=".$hMget['Pid']." AND is_show=1 AND is_show_key=1 AND is_fen_ge=0 AND is_pi_pei=0 AND sup IS NULL AND url IS NULL AND `key` IN (".$fileds_array.")";
              $detail=Db::query($sql);
              $data=array();
              foreach ($detail as $k => $v) {
                $data[$v['key']]=$v['value'];
              }
              $this->redis->hMSet($value['id'],$data);
            }
          }
        }
      });
      //$this->set_tag();
    }


  public function set_tag(){
      Db::table('lily_products')->field('id,part_number,url_product,class_id,img')->chunk(100, function($redis_zhu) {
        foreach ($redis_zhu as $key => $value) {
          $s=explode('_', $value['url_product']);
          if(count($s)<=1){
            $hMget=$this->redis->hMget($value['id'],['Part Number','class_id']);
            $mian = get_crumbs($hMget['class_id']);
            if(isset($mian[1])){
              $max_class=$mian[1];
            }else{
              $max_class=$mian[0];
            }
            $max_class_new=array();

            $maximg=Db::table('lily_classify')->field('img')->where([['id','=',$max_class['id']]])->find();
            if(!empty($maximg['img'])){
              $max_class_new['img']=$this->images_host.$maximg['img'];
            }else{
              $max_class_new['img']=$this->images_host.'/blog/blogdefault.png';
            }
            $max_class_new['cname']=$mian[1]['cname'];
            $max_class_new['url']='/'.$mian[1]['crumbs_url'];
            $tag_arr=array();
            foreach ($mian as $k => $v) {
              $tag_arr[$v['cname']]='/'.$v['crumbs_url'];
            }
            /*数组转json 再加密*/
            $data=array();
            $data['tag']=base64_encode(json_encode($tag_arr));
            $data['maxclass']=base64_encode(json_encode($max_class_new));
            $this->redis->hMSet($value['id'],$data);
          }
        }
      });
  }

  public function set_brand(){
      Db::table('lily_products')->field('id,part_number,url_product,class_id,img')->chunk(100, function($redis_zhu) {
        foreach ($redis_zhu as $key => $value) {
          $s=explode('_', $value['url_product']);
          if(count($s)<=1){
            $hMget=$this->redis->hMget($value['id'],['Pid']);
            $equivalent=Db::table('lily_equivalent')->field('name,url')->where('pid','=',$hMget['Pid'])->find();
            $data=array();
            if(!empty($equivalent)){
              $data['brand_url']=$equivalent['url'];
              $data['brand']=$equivalent['name'];
              $this->redis->hMSet($value['id'],$data);
            }
          }
        }
      });
  }
  public function cross(){
      ini_set('max_execution_time', '0');
      $redis_zhu=$this->redis->keys("*");
      $i=count($redis_zhu)+1;

      $sql="SELECT cross_number,pid FROM lily_cross WHERE cross_number!='-' AND pid !='' GROUP BY cross_number";
      $cross_number=Db::query($sql);
      foreach ($cross_number as $key => $value) {
        $part_number=Db::table('lily_products')->field('part_number,id')->where('part_number','=',$value['cross_number'])->find();
        if(empty($part_number)){
            $pid_data=explode(',', $value['pid']);
            foreach ($pid_data as $k => $v) {
             $redis_key=$this->redis->keys($v);
             if(!empty($redis_key)){
                $hMget=$this->redis->hgetall($v);
                $new_data=array();
                foreach ($hMget as $k1 => $v1) {
                  if($k1!='Part Number'){
                    $new_data[$k1]=$v1;
                  }else{
                    $new_data['Part Number']=$value['cross_number'];
                  }
                }
                $this->redis->hMSet($i,$new_data);
                $i++;
                break;
             }
            }
        }
      }
  }

  public function set_standard_reference(){
      ini_set('max_execution_time', '0');
      $redis_zhu=$this->redis->keys("*");
      $i=count($redis_zhu)+1;

      foreach ($redis_zhu as $key => $value) {
          $hMget=$this->redis->hMget($value,['Standard Reference']);

          if($hMget['Standard Reference']!=false){
              $new_data=array();

              $hMget1=$this->redis->hgetall($value);
              
              foreach ($hMget1 as $k1 => $v1) {
                  if($k1!='Part Number'){
                    $new_data[$k1]=$v1;
                  }else{
                    $new_data['Part Number']=$hMget['Standard Reference'];
                  }
              }

              $this->redis->hMSet($i,$new_data);
              $i++;
          }
      }
  }


     public function ceshi(){
       $redis_zhu=$this->redis->keys("*");
       $b=array();
       foreach ($redis_zhu as $key => $value) {
          $hMget=$this->redis->hMget($value,['tag']);
         
          $a=json_decode(base64_decode($hMget['tag']),true);
          
          foreach ($a as $k => $v) {
            if(!in_array($k,$b)){
              $b[]=$k;
            }
          }
         
       }
       dump($b);
    }


  public function set_standard_reference1(){
      ini_set('max_execution_time', '0');
      $redis_zhu=$this->redis->keys("*");
      

      foreach ($redis_zhu as $key => $value) {
          $hMget=$this->redis->hMget($value,['Part Number']);
          if($hMget['Part Number']=='SSW20AK' || $hMget['Part Number']=='MS27649-20'){
            echo $value;
              $hMget1=$this->redis->hgetall($value);
              dump($hMget1);
             
          }
      }
  }
    
 

    
    
}